import axios from 'axios';
import VueAxios from 'vue-axios'
import router from '../router/index'
import { MessageBox } from 'element-ui';
import store from '../vuex/store'
axios.interceptors.request.use(
  config => {
    if (store.state.auth.token) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers['nochat-token'] = store.state.auth.token;
    }
    if (store.state.auth.wechatId) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers['wechatId'] = store.state.auth.wechatId;
    }
    return config;
  });
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      if (error.response.status===403){
        MessageBox.alert('请重新登陆', '登陆超时', {
          confirmButtonText: '确定',
          callback: action => {
            router.push('/login');
          }
        });
        MessageBox.alert('登陆超时',"请重新登陆").then(action => {
          router.push('/login');
        });
      }else if(error.response.status===405){
        MessageBox.alert('请稍后再试', error.response.data.message, {
          confirmButtonText: '确定'
        });
      }
    }
    return Promise.reject(error.response.data);  // 返回接口返回的错误信息
  });

let base="http://nochat.callmesoul.cn/api/admin";

let Api={
  //menu
  uploadUrl:base+"/uploadimg",
  uploadImag:function (params) {
    return axios.post(base+'/uploadimg',params);
  },
  login:function (params) {
    return axios.post(base+'/auth',params);
  },
  getWehcat:function (params) {
    return axios.get(base+`/wechat`,params);
  },
  getBanner:function (params) {
    return axios.get(base+`/banner`,{params:params});
  },
  deleteBanner:function (id) {
    return axios.delete(base+`/banner/`+id);
  },
  createBanner:function (params) {
    return axios.post(base+`/banner`,params);
  },
  getBannerDetail:function (id) {
    return axios.get(base+`/banner/`+id);
  },
  updateBanner:function (params) {
    return axios.put(base+`/banner/`+params.id,params);
  },

  //article
  getArticle:function (params) {
    return axios.get(base+`/article`,{params:params});
  },
  getArticleDetail:function (id) {
    return axios.get(base+`/article/`+id);
  },
  createArticle:function (params) {
    return axios.post(base+`/article`,params);
  },
  updateArticle:function (params) {
    return axios.put(base+`/article/`+params.id,params);
  },
  deleteArticle:function (id) {
    return axios.delete(base+`/article/`+id);
  },

  //menu
  getMenu:function (params) {
    return axios.get(base+`/menu`,{params:params});
  },
  getMenuDetail:function (id) {
    return axios.get(base+`/menu/`+id);
  },
  createMenu:function (params) {
    return axios.post(base+`/menu`,params);
  },
  updateMenu:function (params) {
    return axios.put(base+`/menu/`+params.id,params);
  },
  deleteMenu:function (id) {
    return axios.delete(base+`/menu/`+id);
  },

  //brand
  getBrand:function (params) {
    return axios.get(base+`/brand`,{params:params});
  },
  getBrandDetail:function (id) {
    return axios.get(base+`/brand/`+id);
  },
  createBrand:function (params) {
    return axios.post(base+`/brand`,params);
  },
  updateBrand:function (params) {
    return axios.put(base+`/brand/`+params.id,params);
  },
  deleteBrand:function (id) {
    return axios.delete(base+`/brand/`+id);
  },

  //tag
  getTag:function (params) {
    return axios.get(base+`/tag`,{params:params});
  },
  getTagDetail:function (id) {
    return axios.get(base+`/tag/`+id);
  },
  createTag:function (params) {
    return axios.post(base+`/tag`,params);
  },
  updateTag:function (params) {
    return axios.put(base+`/tag/`+params.id,params);
  },
  deleteTag:function (id) {
    return axios.delete(base+`/tag/`+id);
  },

  //class
  getClass:function (params) {
    return axios.get(base+`/class`,{params:params});
  },
  getClassDetail:function (id) {
    return axios.get(base+`/class/`+id);
  },
  createClass:function (params) {
    return axios.post(base+`/class`,params);
  },
  updateClass:function (params) {
    return axios.put(base+`/class/`+params.id,params);
  },
  deleteClass:function (id) {
    return axios.delete(base+`/class/`+id);
  },


  //class
  getComment:function (params) {
    return axios.get(base+`/articleComment`,{params:params});
  },
  getCommentDetail:function (id) {
    return axios.get(base+`/articleComment/`+id);
  },
  createClass:function (params) {
    return axios.post(base+`/articleComment`,params);
  },
  updateComment:function (params) {
    return axios.put(base+`/articleComment/`+params.id,params);
  },
  deleteComment:function (id) {
    return axios.delete(base+`/articleComment/`+id);
  },
  changeDispaly:function (params) {
    return axios.post(base+`/articleComment/display`,params);
  },

};


export default Api;
